package com.mlm.mlmapp.adapter.view.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.mlm.mlmapp.R;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DialogItemsAdapter extends BaseAdapter {
	private List<JSONObject> list;
	String id;
	Button price;
	String selectedID;
	Context mContext;
	public int selectedPosition = -1;
	List<CheckBox> listRadio = new ArrayList<CheckBox>();
	String selectedType;
	OnCheckedChangeListener listener;
	public JSONObject selectedItem;
	public DialogItemsAdapter(Context context, List<JSONObject> obj) {
		this.list = obj;
		this.mContext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub

		return position;
	}
	public void setSelected(String id){
		this.selectedID=id;
	}
	public void setSelectedType(String type){
		this.selectedType=type;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		final ViewHolder holder;
		final JSONObject json = list.get(position);
		if (json.isNull("init")) {
			if (!json.isNull("productName")) {
				if (convertView != null && convertView.getTag() != null) {
					holder = (ViewHolder) convertView.getTag();
				} else {
					convertView = View.inflate(mContext,
							R.layout.book_dialog_item, null);
					holder = new ViewHolder();
					holder.radio = (CheckBox) convertView
							.findViewById(R.id.radio_combo);
					holder.oPrice=(TextView)convertView.findViewById(R.id.original_price);
					holder.radio.setTag(position);
					holder.price = (Button) convertView
							.findViewById(R.id.btn_price);

					if (!listRadio.contains(holder.radio))
						listRadio.add(holder.radio);
				}
				holder.radio.setText(CommonUtils.getJsonString(json,
						"productName"));
				holder.oPrice.setText(mContext.getString(R.string.money,CommonUtils.getJsonInt(json,"originalPrice")));
				holder.oPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
				if(CommonUtils.getJsonInt(json,"originalPrice")>CommonUtils.getJsonInt(json, "price")){
					holder.oPrice.setVisibility(View.VISIBLE);
				}else{
					holder.oPrice.setVisibility(View.INVISIBLE);
				}

				holder.price.setText(mContext.getString(R.string.money,
						CommonUtils.getJsonInt(json, "price")));
				holder.radio
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								// TODO Auto-generated method stub

								if (isChecked) {
//									for (RadioButton r : listRadio) {
//
//										if (buttonView != r)
//											r.setChecked(false);
//									}
									selectedID = CommonUtils.getJsonString(json,"productId");
									selectedPosition = position;
									selectedItem=json;
									notifyDataSetChanged();
								}else{
									if( CommonUtils.getJsonString(json,"productId").equals(selectedID)){
										selectedID="";
										selectedPosition=-1;
										selectedItem=null;
									}
								}
								notifyDataSetChanged();
							}
						});

				if(selectedID.equals(CommonUtils.getJsonString(json,"productId"))){
						holder.radio.setChecked(true);
				}else {
					holder.radio.setChecked(false);
//					if(holder.radio.isAttachedToWindow()){
////						holder.radio.setChecked(false);
//					}
				}
				convertView.setTag(holder);
			} else {
				return View.inflate(mContext, R.layout.book_dialog_no_item,
						null);
			}
		} else {
			return View
					.inflate(mContext, R.layout.book_dialog_no_item, null);
		}

		return convertView;
	}

	static class ViewHolder {
		CheckBox radio;
		Button price;
		TextView oPrice;
	}
}
